package com.augurit.common.sysconfig.web;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.utils.SpringContextHolder;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.base.action.ExtBaseAction;
import com.augurit.common.base.listener.AosInitListener;
import com.augurit.common.sysconfig.service.ISysConfigService;
import com.augurit.common.sysconfig.web.form.SysConfigForm;
import com.augurit.common.util.ExtUtils;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "sys-config.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/common/sysconfig/sys-config.jsp")
})
public class SysConfigAction extends ExtBaseAction {

	@Autowired
	private ISysConfigService sysConfigService;

	// 基本属性
	private String key;
	private String value;
	private Page<SysConfigForm> page = new Page<SysConfigForm>();


	/**
	 * 系统首页的默认入口方法
	 */
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	/**
	 * 分页获取系统首页配置对象列表
	 */
	public String list() throws Exception {
		ExtUtils.initPageFromExtGridParam(this.getRequest(), page);
		sysConfigService.getAll(page);
		this.extRenderGridJson(page);
		return null;
	}

	/**
	 * 修改保存指定配置项的值
	 * @return
	 * @throws Exception
	 */
	public String save() throws Exception {
		sysConfigService.updateItem(key, value);
		
		//重新加载系统初始化配置信息
		AosInitListener.loadAosInitConfig(this.getApplication(), SpringContextHolder.getApplicationContext());
		
		this.extRenderSuccess();
		return null;
	}
	
	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}
}
